package table

import (
	"context"
	"time"

	"go-weixin/app/usercenter/api/internal/svc"
	"go-weixin/app/usercenter/api/internal/types"
	"go-weixin/app/usercenter/rpc/pb/proto/usercenter"

	"github.com/zeromicro/go-zero/core/logx"
)

type TableListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewTableListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TableListLogic {
	return &TableListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *TableListLogic) TableList(req *types.TableListReq) (resp *types.TableListResp, err error) {
	ctx, canelFunc := context.WithTimeout(context.Background(), 5*time.Second)
	defer canelFunc()
	res, err := l.svcCtx.UserRpc.TableList(ctx, &usercenter.TableListReq{
		Page: req.Page,
	})
	if err != nil {
		return nil, err
	}

	var tableList []types.Table
	if len(res.TableList) > 0 {
		for _, table := range res.TableList {
			tableList = append(tableList, types.Table{
				Id:       table.Id,
				Name:     table.Name,
				Url:      table.Url,
				Method:   table.Method,
				CreateAt: table.CreateAt,
				UpdateAt: table.UpdateAt,
			})
		}
	}
	resp = new(types.TableListResp)
	resp.TableList = tableList
	return
}
